<?php
	class crud_ntteetime {
		function getAllNtTeeTime(){
			$start	= (isset($_REQUEST['start']))? $_REQUEST['start'] : 0;
			$limit	= (isset($_REQUEST['limit']))? $_REQUEST['limit'] : 10;
			$sql = "select 
						tt.*,
						date_format(tt.TEE_START,'%d/%m/%Y')as DATE_START,
						date_format(tt.TEE_START,'%H:%i')as TIME_START,
						date_format(tt.TEE_START,'%d/%m/%Y %H:%i')as DATE_TIME_START,
						ls.ID as LIVE_SCORE_ID,
						pl.`PLAYER`,
						gc.`ID` as GOLF_COURSE_ID,
						gc.NAME as NAME_GOLF_COURSE
					from tee_time tt
					inner join live_score ls on tt.ID = ls.TEE_TIME_ID
					inner join player pl on pl.ID = tt.PLAYER_ID
					inner join golf_course gc on gc.ID = ls.GOLF_COURSE_ID where 1=1 ";
			$sqlCount = "select 
							count(*) as total
					from tee_time tt
					inner join live_score ls on tt.ID = ls.TEE_TIME_ID
					inner join player pl on pl.ID = tt.PLAYER_ID
					inner join golf_course gc on gc.ID = ls.GOLF_COURSE_ID where 1=1 ";
			if (isset($_GET['query'])){
				$sql .= "and (
							pl.`PLAYER` like '%".$_GET['query']."%' or
							gc.NAME like '%".$_GET['query']."%'
						)";
				$sqlCount .= "and (
							pl.`PLAYER` like '%".$_GET['query']."%' or
							gc.NAME like '%".$_GET['query']."%'
						)";
			}
			$sql .= " order by tt.`TEE_START` LIMIT ".$limit." OFFSET ".$start;
			$resultSql = Yii::app()->db->createCommand($sql)->queryAll();
			$resultCount = Yii::app()->db->createCommand($sqlCount)->queryAll();
			echo '{success:true,results: '.$resultCount[0]['total'].',rows:'.json_encode($resultSql).'}';
		}
		
		function insert(){
			$TeeTime = new TeeTime;
			$LiveScore = new LiveScore;
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$TeeTime->TEE_START = Yii::app()->helper->dateFormating($_POST['datestart-inputEl'])." ".$_POST['timestart-inputEl'];
				$TeeTime->PLAYER_ID = $_POST['id_player-inputEl'];
				$TeeTime->save();
				
				$LiveScore->TEE_TIME_ID = $TeeTime->ID;
				$LiveScore->GOLF_COURSE_ID = $_POST['id_golfcourse-inputEl'];
				$LiveScore->save();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Insert Sukses","code":"1","ID":"'.$TeeTime->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Insert Gagal","code":"0"}]}';
			}
			echo $feedback;
		}
		
		function update(){
			$TeeTime = TeeTime::model()->findByPk($_POST['id_teetime-inputEl']);
			$LiveScore = LiveScore::model()->findByPk($_POST['id_livescore-inputEl']);
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$TeeTime->TEE_START = Yii::app()->helper->dateFormating($_POST['datestart-inputEl'])." ".$_POST['timestart-inputEl'];
				$TeeTime->PLAYER_ID = $_POST['id_player-inputEl'];
				$TeeTime->save();
				
				$LiveScore->GOLF_COURSE_ID = $_POST['id_golfcourse-inputEl'];
				$LiveScore->save();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Update Sukses","code":"1","ID":"'.$TeeTime->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Update Gagal","code":"0"}]}';
			}
			echo $feedback;
		}
		
		function delete(){
			$TeeTime = TeeTime::model()->findByPk($_POST['id_teetime']);
			$LiveScore = LiveScore::model()->findByPk($_POST['id_livescore']);
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$LiveScore->delete();
				$TeeTime->delete();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Berhasil Dihapus","code":"1","ID":"'.$TeeTime->ID.'"}]}';
			} catch (Exception $e) {
				echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Gagal Dihapus","code":"0"}]}';
			}
			echo $feedback;
		}
	}
?>